//
//  MyViewController.m
//  Products_B
//
//  Created by lanou on 16/5/27.
//  Copyright © 2016年 朋学友. All rights reserved.
//

#import "MyViewController.h"
#import "MyTableViewCell.h"
#import "LikeViewController.h"
#import "SetViewController.h"
#import "PersonalSetTableViewController.h"
#import "LoginViewController.h"
@interface MyViewController () <UITableViewDataSource, UITableViewDelegate> {
    UILabel *_label1;
    UILabel *_label2;
    UILabel *_label3;
}

@property (strong, nonatomic) UITableView *tableView;
@property (nonatomic, strong) NSArray *modelArray;

@end

@implementation MyViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    _modelArray = @[@"我的消息",@[@"我的订单",@"我的红包",@"我的收藏"],@"我的缓存",@"喜欢",@"邀请好友"];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight - 50) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.rowHeight = 60;
    [self.view addSubview:_tableView];
    
    // 执行以下代码，隐藏状态栏
    if ([self respondsToSelector:@selector(edgesForExtendedLayout)]) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
    
    // 注册
    [_tableView registerClass:[MyTableViewCell class] forCellReuseIdentifier:@"myTableViewCell"];
    
    [self layoutHeaderView];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}

- (void)layoutHeaderView {
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 40 + 292 * kWidth / 548)];
    [self.view addSubview:headerView];
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 292 * kWidth / 548)];
    imageView.image = [UIImage imageNamed:@"MyPicture.jpg"];
    [headerView addSubview:imageView];

    UILabel *topLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 27, kWidth, 30)];
    topLabel.textAlignment = NSTextAlignmentCenter;
    topLabel.text = @"我的";
    topLabel.font = [UIFont systemFontOfSize:15];
    [headerView addSubview:topLabel];

    UIButton *setButton = [UIButton buttonWithType:UIButtonTypeCustom];
    setButton.frame = CGRectMake(kWidth - 50, 17, 20, 20);
    [setButton setImage:[UIImage imageNamed:@"MySet.png"] forState:UIControlStateNormal];
    [setButton addTarget:self action:@selector(setButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:setButton];
    
    _label1 = [[UILabel alloc] initWithFrame:CGRectMake(30, headerView.frame.size.height - 30, (kWidth - 60 - 160) / 3, 30)];
    _label1.alpha = 0.6;
    _label1.font = [UIFont systemFontOfSize:14];
    _label1.text = @"作品(0)";
    [headerView addSubview:_label1];
    
    _label2 = [[UILabel alloc] initWithFrame:CGRectMake(30 + _label1.frame.size.width + 80, headerView.frame.size.height - 30, (kWidth - 60 - 160) / 3, 30)];
    _label2.alpha = 0.6;
    _label2.font = [UIFont systemFontOfSize:14];
    _label2.text = @"关注(0)";
    [headerView addSubview:_label2];
    
    _label3 = [[UILabel alloc] initWithFrame:CGRectMake(_label2.frame.origin.x + _label2.frame.size.width + 80, headerView.frame.size.height - 30, (kWidth - 60 - 160) / 3, 30)];
    _label3.alpha = 0.6;
    _label3.font = [UIFont systemFontOfSize:14];
    _label3.text = @"粉丝(0)";
    [headerView addSubview:_label3];
    
    // 这个view用来添加子控件
    UIView *transparentView = [[UIView alloc] initWithFrame:CGRectMake(0, imageView.frame.size.height * 3 / 5, kWidth, imageView.frame.size.height * 2 / 5)];
    transparentView.backgroundColor = [UIColor clearColor];
    // 这个view在transparentView和子控件中间，将其设置为半透明，子控件全部加在transparentView上面，这样改变backgroundView的alpha值就不会影响到transparentView上的子控件
    UIView *backgroundView = [[UIView alloc] initWithFrame:transparentView.bounds];
    backgroundView.backgroundColor = [UIColor blackColor];
    backgroundView.alpha = 0.5;
    [transparentView addSubview:backgroundView];
    
    UIImageView *header_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(30, - 30, 60, 60)];
    header_imageView.backgroundColor = [UIColor redColor];
    header_imageView.layer.cornerRadius = 30;
    header_imageView.layer.masksToBounds = YES;
    header_imageView.userInteractionEnabled = YES;
    [transparentView addSubview:header_imageView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [header_imageView addGestureRecognizer:tap];
    
    CGFloat x = header_imageView.frame.origin.x + header_imageView.frame.size.width + 10;
    UILabel *nickLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, 10, kWidth - x , 30)];
    nickLabel.text = @"昵称";
    nickLabel.font = [UIFont systemFontOfSize:16];
    nickLabel.textColor = [UIColor whiteColor];
    [transparentView addSubview:nickLabel];
    
    UILabel *jieshaoLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, header_imageView.frame.origin.y + header_imageView.frame.size.height + 10, kWidth - 60, 30)];
    jieshaoLabel.text = @"每个吃货都是有背景的，介绍一下自己";
    jieshaoLabel.textColor = [UIColor whiteColor];
    jieshaoLabel.font = [UIFont systemFontOfSize:14];
    [transparentView addSubview:jieshaoLabel];
    [headerView addSubview:transparentView];

    [_tableView setTableHeaderView:headerView];
}

// 点击设置按钮
- (void)setButtonAction:(UIButton *)button {
    SetViewController *setVC = [[SetViewController alloc] init];
    [self.navigationController pushViewController:setVC animated:YES];
}

- (void)tapAction:(UITapGestureRecognizer *)tap {
    NSString *userIdString = [[NSUserDefaults standardUserDefaults] objectForKey:@"user_id"];
    if (userIdString) {
        PersonalSetTableViewController *personalSetVC = [[PersonalSetTableViewController alloc] init];
        [self.navigationController pushViewController:personalSetVC animated:YES];
    } else {
        LoginViewController *loginVc = [[LoginViewController alloc] init];
        [self.navigationController pushViewController:loginVc animated:YES];
    }
}

// 返回分区个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _modelArray.count;
}

// 返回每个的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 1) {
        NSArray *arr = _modelArray[1];
        return arr.count;
    }
    return 1;
}

// 返回cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MyTableViewCell *cell = [MyTableViewCell cellWithTableView:tableView indexPath:indexPath];
    // indexPath赋值一定要在数组赋值前面
    cell.indexPath = indexPath;
    cell.modelArray = _modelArray;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        
    }
    if (indexPath.section == 3) {
        LikeViewController *likeVC = [[LikeViewController alloc] init];
        [self.navigationController pushViewController:likeVC animated:YES];
    }
    if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            NSString *userIdString = [[NSUserDefaults standardUserDefaults] objectForKey:@"user_id"];
            if (userIdString) {
                // 进行跳转
            } else {
                LoginViewController *loginVc = [[LoginViewController alloc] init];
                [self.navigationController pushViewController:loginVc animated:YES];
            }
            
        } else if (indexPath.row == 1) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://m.izhangchu.com/voucher/mypurse.html?app_hideheader=1,status:1,login:1"]];
        } else {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://m.izhangchu.com/my/collect.html?app_hideheader=1&app_exitpage=1,status:1,login:1"]];
        }
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
